package com.ztman.rxjava.mvp.presenter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.ztman.rxjava.BaseFragment;
import com.ztman.rxjava.mvp.view.BaseViewActivityIml;

/**
 * Created by YJH on 2017/11/30.
 * 功能：针对 比如：有五个tab的Activity嵌入Fragment；内容的显示通过show()和hide()进行控制的懒加载方式；
 * 抽象出来的视图层，逻辑功能处理的代理
 */

public abstract class FragmentSwitchPresenter<T extends BaseViewActivityIml> extends BaseFragment {
    //是否在Fragment使用沉浸式
    protected static final boolean isImmersionBarEnabled = true;
    protected ImmersionBar mImmersionBar;
    protected T fmtPresenter;

    /**
     * 功能：这个是关键，这个方法会在终端Fragment类中实现；从而激活所有的方法
     * @return
     */
    protected abstract Class<T> getPresenterClass();
    public FragmentSwitchPresenter(){
        try {
            fmtPresenter = getPresenterClass().newInstance();
        } catch (java.lang.InstantiationException e) {
            throw new RuntimeException("create BaseViewActivityIml error in FragmentPresenter.");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create BaseViewActivityIml error in FragmentPresenter.");
        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fmtPresenter.createInIBaseView(inflater, null, savedInstanceState);
        return fmtPresenter.getRootViewInIBaseView();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isImmersionBarEnabled)
            initImmersionBar();
        fmtPresenter.InitWidgetInIBaseView();
        initData();
        initView();
    }

    protected abstract void initView();

    protected abstract void initData();

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (fmtPresenter == null) {
            try {
                fmtPresenter = getPresenterClass().newInstance();
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null)
            mImmersionBar.init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fmtPresenter = null;
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

}
